package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener;
import com.google.android.gms.maps.model.Marker;
import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.queries.QueriesConstant.TableUser;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.ui.Section;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class thongbaomoi extends Activity {
	public ImageLoader image;
	public ImageLoader imageLoader;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thongbaomoi);
		ParseUser user = ParseUser.getCurrentUser();
		ImageView profileImage = (ImageView) findViewById(R.id.left_menu_pic);
		
		imageLoader = ImageLoader.getInstance();

		imageLoader = ImageLoader.getInstance();
		DisplayImageOptions options1 = new DisplayImageOptions.Builder()
				.cacheInMemory(true).cacheOnDisc(true)
				.resetViewBeforeLoading(true).build();
		// Initialize ImageLoader with configuration. Do it once.
		imageLoader.init(ImageLoaderConfiguration
				.createDefault(getBaseContext()));
		imageLoader.displayImage(user.getString(TableUser.COLUMN_photoURL),
				profileImage, options1);
		
		image = ImageLoader.getInstance();
		final ImageView im = (ImageView) findViewById(R.id.anhbia);
		image = ImageLoader.getInstance();
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.cacheInMemory(true).cacheOnDisc(true)
				.resetViewBeforeLoading(true).build();
		// Initialize ImageLoader with configuration. Do it once.
		image.init(ImageLoaderConfiguration.createDefault(getBaseContext()));
		image.displayImage(user.getString(TableUser.COLUMN_anhbia), im, options);

		TextView profileFullName = (TextView) findViewById(R.id.left_menu_fullname);
		profileFullName.setText(user.getString(TableUser.COLUMN_firstName)
				+ " " + user.getString(TableUser.COLUMN_lastName));

		final ListView lv = (ListView) findViewById(R.id.lv_thongbaomoi);
		/*
		 * event click profileImage
		 */
		profileImage.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ParseUser user = ParseUser.getCurrentUser();
				Intent intent = new Intent(thongbaomoi.this,
						thongtincanhan.class);
				IntentHelper.addObjectForKey(user, "user");
				startActivity(intent);
				
			}
		});
		
		//final String id = user.getString(QueriesConstant.TableUser.COLUMN_objectId).toString();
		ParseQuery<ParseObject> query = ParseQuery.getQuery("TinNhan");
		query.whereEqualTo("user_id",user.getObjectId().toString());
		query.findInBackground(new FindCallback<ParseObject>() {
			@Override
			public void done(List<ParseObject> tinnhans, ParseException e) {
				ArrayList<Model> models = new ArrayList<Model>();
				for (final ParseObject tinnhan : tinnhans) {
					
					if (tinnhan.getInt("xem") == 0)					
						if (tinnhan.getString("noidung").length()>20)
						models.add(new Model(R.drawable.thongbao, tinnhan
								.getString("noidung").substring(0, 20)
								+ "..."
								+ "(!)", tinnhan.getString("noidung")
								.toString()));
						else 
							models.add(new Model(R.drawable.thongbao, tinnhan
									.getString("noidung") 
									
									+ "(!)", tinnhan.getString("noidung")
									.toString()));
					else {
						if (tinnhan.getString("noidung").length()>20)
						models.add(new Model(R.drawable.thongbao, tinnhan
								.getString("noidung").substring(0, 20) + "...",
								tinnhan.getString("noidung").toString()));
						else 
							models.add(new Model(R.drawable.thongbao, tinnhan
									.getString("noidung"),
									tinnhan.getString("noidung").toString()));
					}

				}

				MyAdapter1 adapter = new MyAdapter1(thongbaomoi.this, models,
						R.color.mau_nen);
				lv.setAdapter(adapter);

			}
		});
		// models.add(new Model(R.drawable.thongbao,"Thông báo mới ","1"));
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// thong bao moi
				Intent intent = new Intent(thongbaomoi.this,
						noidungthongbao.class);
				View v = lv.getAdapter().getView(position, null, lv);
				TextView t = (TextView) findViewById(R.id.item_counter);
				intent.putExtra("noidung", t.getText().toString());
				startActivity(intent);

			}
		});

	}

}
